package net.zjitc.service;

import net.zjitc.mapper.UserMapper;
import net.zjitc.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2018/1/3.
 */
public class UserService {
    //UserMapper是动态代理出来的

    SqlSessionFactory factory;

    public UserService() {
         /*读取资源配置文件*/
        InputStream in= null;
        try {//                                         SQL配置
            in = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*根据资源文件生成SessionFactory*/
        factory=new SqlSessionFactoryBuilder().build(in);
        /*从session工厂中打开session会话*/
    }


    public List<User> findAll() {
        SqlSession sqlSession=factory.openSession();
        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        List<User> list= mapper.findAllUser();
        return list;
        /**
         * 如果是添加，修改这里需要
         *  sqlSession.commit();
         *   sqlSession.close();
         */
    }
}
